﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for CommentsDAL
/// </summary>
public class CommentsDAL: StandardConnectDB, IComments
{
	public CommentsDAL()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public List<Comments> GetListCommentsOfPost(Posts post)
    {
        //Create object listComments
        List<Comments> listComments = new List<Comments>();

        //Create object paramList store parameter execute store procedure
        Dictionary<String, Object> paramList = new Dictionary<String, Object>();

        //Add parameter pIdPost to paramList
        paramList.Add("@pIdPost", post.IdPost);

        //Get Comments by store procedure
        DataTable dt = this.DTExecuteSelect("procGetListCommentsOfPostByIdPost", paramList);

        //Add Comments from DataTable to listComments
        foreach (DataRow dr in dt.Rows)
        {
            //Convert objec DataRow to object Comments and add to listComments
            listComments.Add(this.ConvertObjectDataRowToComment(dr));
        }

        return listComments;
    }

    public List<Comments> GetListCommentsOfUsersAndPost(Users user, Posts post)
    {
        //Create object listComments
        List<Comments> listComments = new List<Comments>();

        //Create object paramList store parameter execute store procedure
        Dictionary<String, Object> paramList = new Dictionary<String, Object>();

        //Add param to list
        paramList.Add("@pUsername", user.Username);
        paramList.Add("@pIdPost", post.IdPost);

        //Get Comments by store procedure
        DataTable dt = this.DTExecuteSelect("procGetListCommentsOfUsersAndPost", paramList);

        //Add Comments from DataTable to listComments
        foreach (DataRow dr in dt.Rows)
        {
            //Convert object DataRow to object Comments and add to listComments
            listComments.Add(this.ConvertObjectDataRowToComment(dr));
        } 

        return listComments;
    }

    public List<UserProfiles> GetListCommentByIdPost(int idpost)
    {
        List<UserProfiles> listcommnemt = new List<UserProfiles>();
        Dictionary<String, Object> paramist = new Dictionary<string, object>();
        paramist.Add("@pIdPost", idpost);
        DataTable dt = this.DTExecuteSelect("spGetCommentbyIdPost", paramist);
        foreach (DataRow dr in dt.Rows)
        {
            listcommnemt.Add(this.ConvertObjectDataRow(dr));
        }
        return listcommnemt;
    }

    private UserProfiles ConvertObjectDataRow(DataRow dr)
    {
        UserProfiles u = new UserProfiles();
        Comments c = new Comments();
        u.User.Username = dr["Username"].ToString();
        u.Avatar = dr["Avatar"].ToString();
        c.Contents = dr["Contents"].ToString();
        return u;


    }

    public bool CreateComment(Users user, string contents, bool privacy, Posts post)
    {
        //Create object result
        bool result = false;

        //Create param list store parameter execute store procedure
        Dictionary<string, object> paramList = new Dictionary<string, object>();

        //Add param to list
        paramList.Add("@pUsername", user.Username);
        paramList.Add("@pContents", contents);
        paramList.Add("@pPrivacy", privacy);
        paramList.Add("@pIdPost", post.IdPost);

        //Insert object Comments to table Comments in database by store procedure
        result = this.ExecuteUpdate("procCreateComment", paramList);

        return result;
    }


    public bool DeleteComment(Comments comment)
    {
        //Create object result
        bool result = false;

        //Create param list store parameter execute store procedure
        Dictionary<string, object> paramList = new Dictionary<string, object>();

        //Add param to list
        paramList.Add("@pIdComment", comment.IdComment);

        //Update object Comments by store procedure
        result = this.ExecuteUpdate("procDeleteComment", paramList);

        return result;
    }

    public Comments ConvertObjectDataRowToComment(System.Data.DataRow dr)
    {
        //Create object Messages
        Comments comment = new Comments();

        //Set properties of Messages
        comment.IdComment = int.Parse(dr["IdComment"].ToString());
        comment.Contents = dr["Contents"].ToString();
        comment.Privacy = bool.Parse(dr["Privacy"].ToString());
        comment.CreateTime = DateTime.Parse(dr["CreateTime"].ToString());

        //Get Users
        string user = dr["Username"].ToString();
        //Create object IUsers
        IUsers iUser = new UsersDAL();
        //Set property User of Comments
        comment.User = iUser.GetUserByUsername(user);

        //Get Posts
        int idPost = int.Parse(dr["IdPost"].ToString());
        //Create object IPosts
        IPosts iPost = new PostsDAL();
        //Set property Post of Comments
        comment.Post = iPost.GetPostByIdPost(comment.User, idPost);

        return comment;
    }


    public DataTable GetDataTableCommentsOfPost(int idPost)
    {
        DataTable dt = this.DTExecuteSelect("SELECT up.Avatar, u.Username, c.Contents FROM Users u JOIN UserProfiles up ON u.UserName = up.Username JOIN Comments c ON u.UserName = c.Username WHERE c.IdPost = " + idPost);

        return dt;
    }

    public DataTable GetDataTableCommnetByIdPost(int idPost)
    {
        return null;
    }

}